package com.alibaba.aone.casestudy.service.impl;

import java.util.List;

import org.apache.ecs.xhtml.ol;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.CaseStudyConditions;
import com.alibaba.aone.casestudy.common.entity.Casestudy;
import com.alibaba.aone.casestudy.common.entity.CasestudyAnalysisParticipant;
import com.alibaba.aone.casestudy.dao.CasestudyDao;
import com.alibaba.aone.casestudy.service.CasestudyService;

public class CasestudyServiceImpl implements CasestudyService {

	@Autowired
	private CasestudyDao casestudyDao;

	public List<Casestudy> listCasestudys(CaseStudyConditions conditions) {
		return casestudyDao.listCasestudys(conditions);
	}

	public Casestudy getCasestudyByNumber(String casestudyNo) {
		return casestudyDao.loadCasestudy(casestudyNo);
	}

	public List<Casestudy> search(CaseStudyConditions caseStudyConditions) {
		return casestudyDao.search(caseStudyConditions);
	}

	public List<Casestudy> getAllCasestudies() {
		return null;// return casestudyDao.getAll();
	}

	public List<CasestudyAnalysisParticipant> getAnalysisPartisByCasestudyno(
			String csno) {
		return casestudyDao.findAnalysisPartisByCasestudyno(csno);
	}

	public void updateAnalysisPartis(
			List<CasestudyAnalysisParticipant> newPartis, String csno) {
		List<CasestudyAnalysisParticipant> oldPartis = getAnalysisPartisByCasestudyno(csno);
		for (CasestudyAnalysisParticipant parti : newPartis) {
			if (!oldPartis.contains(parti)) {
				this.addAnalysisParti(parti);
			}
		}
		for (CasestudyAnalysisParticipant parti : oldPartis) {
			if (!newPartis.contains(parti)) {
				this.removeAnalysisParti(parti);
			}
		}
	}

	public void addAnalysisParti(CasestudyAnalysisParticipant parti) {
		casestudyDao.insertAnalysisParti(parti);
	}

	public int removeAnalysisParti(CasestudyAnalysisParticipant parti) {
		return casestudyDao.deleteAnalysisParti(parti);
	}

}
